import random
import time


# class Person:
#
#     def __init__(self, name, age):
#         self.name = name
#         self.age = age
#
#     def __call__(self, *args, **kwargs):
#         print(f'{self.name}可以调用啦')
#
#
# p = Person('Wang', 20)
# p()


# 用类作为装饰器：相对于用一个可调用对象替换掉被装饰的函数
class TimeRecorder:

    def __init__(self, func):
        self.func = func

    def __call__(self, *args, **kwargs):
        start = time.time()
        result = self.func(*args, **kwargs)
        end = time.time()
        print(f'执行时间: {end - start:.3f}秒')
        return result


# 用函数作为装饰器：用一个函数把被装饰器的函数替换掉
# def record_time(func):
#
#     def wrapper(*args, **kwargs):
#         start = time.time()
#         result = func(*args, **kwargs)
#         end = time.time()
#         print(f'执行时间: {end - start:.3f}秒')
#         return result
#
#     return wrapper


# download = record_time(download)
# @record_time
# download = TimeRecorder(download)
@TimeRecorder
def download(filename):
    print(f'开始下载{filename}')
    time.sleep(random.randint(3, 8))
    print(f'{filename}下载完成')


print(type(download))
download('Python从入门到住院')
